<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="850px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <div class="formTopFont">员工登记单</div>
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="80px">
        <el-tabs type="card" tab-position="top">
          <el-tab-pane label="基本信息">
            <el-row>
              <el-col :span="8">
                <el-form-item label="账号" prop="account">
                  <el-input v-model.trim="ruleForm.account" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="密码" prop="password">
                  <el-input v-model.trim="ruleForm.password" maxlength="20" show-word-limit type="password" show-password />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="员工姓名" prop="name">
                  <el-input v-model="ruleForm.name" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="员工编号" prop="code">
                  <el-input v-model.trim="ruleForm.code" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="性别" prop="sex">
                  <el-input v-model.trim="ruleForm.sex" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="出生日期" prop="birthday">
                  <el-date-picker v-model="ruleForm.birthday" type="datetime" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="选择日期时间"></el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="已婚" prop="married">
                  <el-select v-model="ruleForm.married" clearable>
                    <el-option value="是" label="是"></el-option>
                    <el-option value="否" label="否"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="籍贯" prop="nativePlace">
                  <el-input v-model.trim="ruleForm.nativePlace" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="民族" prop="ethnicGroup">
                  <el-input v-model.trim="ruleForm.ethnicGroup" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="身份证号" prop="idCard">
                  <el-input v-model.trim="ruleForm.idCard" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="政治面貌" prop="political">
                  <el-input v-model.trim="ruleForm.political" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="家庭住址" prop="address">
                  <el-input v-model.trim="ruleForm.address" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="学历" prop="education">
                  <el-input v-model.trim="ruleForm.education" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="手机号" prop="telephone">
                  <el-input v-model.trim="ruleForm.telephone" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="邮箱" prop="email">
                  <el-input v-model.trim="ruleForm.email" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <el-row>
              <el-col :span="12">
                <el-form-item label="入职日期" prop="joinedDate">
                  <el-date-picker v-model="ruleForm.joinedDate" type="datetime" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="选择日期时间"></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="班别" prop="classes">
                  <el-input v-model="ruleForm.classes" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="所属部门" prop="deptId">
                  <tree-select ref="treeSelect" style="width: 100%" :props="treeProps" :options="departData" :disabled="false" :accordion="true" :value="ruleForm.deptId" search-key="deptName" @getNode="getValue($event)" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="目前岗位" prop="post">
                  <el-input v-model="ruleForm.post" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="合同到期" prop="expirationDate">
                  <el-date-picker v-model="ruleForm.expirationDate" type="datetime" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="选择日期时间"></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="受聘状态" prop="employed">
                  <el-select v-model="ruleForm.employed" clearable>
                    <el-option value="是" label="在职"></el-option>
                    <el-option value="否" label="离职"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="ruleForm.remark" type="textarea" rows="3" maxlength="500" show-word-limit />
              </el-form-item>
            </el-col>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-row>
  </el-dialog>
</template>
<script>
import TreeSelect from '@/components/TreeSelect'
export default {
  components: { TreeSelect },
  data() {
    const checkCode = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/comp/userInfo/verifyCodeUnique?code=${value}&id=${
          this.ruleForm.id || ''
        }`
      )
      if (res?.data.code === 200) {
        return res.data.data
          ? callback()
          : new Error(callback('员工编号重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    const checkAccount = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/comp/userInfo/verifyAccountUnique?account=${value}&id=${
          this.ruleForm.accountUserId || ''
        }`
      )
      if (res?.data.code === 200) {
        return res.data.data ? callback() : new Error(callback('账号重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    return {
      title: '',
      deptList: [],
      ruleForm: {
        name: '',
        account: '',
        remark: '',
        post: ''
      },
      treeProps: {
        value: 'id',
        label: 'deptName',
        children: 'children'
      },
      rules: {
        name: [
          { required: true, message: '员工名称不能为空！', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '员工编号不能为空！', trigger: 'blur' },
          { validator: checkCode, trigger: 'blur' }
        ],
        account: [
          { required: true, message: '账号不能为空！', trigger: 'blur' },
          { validator: checkAccount, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空！', trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      departData: []
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      this.dialogVisible = true
      await this.getDepartData()
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        this.ruleForm = Object.assign({}, row)
      } else {
        this.title = '新增'
      }
    },
    /**
     * @description: 获取所有部门
     * @return {*}
     */
    async getDepartData() {
      const res = await this.$axios.get('/dept/list')
      this.departData = [{ id: '0', deptName: '无' }, ...res.data.data]
    },
    getValue(val) {
      // console.log(val)
      this.ruleForm.deptId = val.id
      this.ruleForm.eptName = val.deptName
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await this.$axios.post(
            '/comp/userInfo/save',
            this.ruleForm
          )
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        name: '',
        account: '',
        remark: '',
        post: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
